using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace SupplyChainWebE9
{
    // DXCOMMENT: Configure a data model (In this sample, we do this in file NorthwindDataProvider.cs. You would better create your custom file for a data model.)
    public static class SCMDataProvider
    {
        const string SCMDataContextKey = "DXSCMDataContext";

        public static SCMDataContext DB
        {
            get
            {
                if (HttpContext.Current.Items[SCMDataContextKey] == null)
                    HttpContext.Current.Items[SCMDataContextKey] = new SCMDataContext();
                return (SCMDataContext)HttpContext.Current.Items[SCMDataContextKey];
            }
        }

        public static IEnumerable GetSugPoDtl(string vendorID)
        {
            string[] vens = vendorID.Split('~');
            return from sugpodtl in DB.SugPoDtl where vens.Contains(sugpodtl.VendorID) && sugpodtl.CheckBox05 == false select sugpodtl;
        }
        public static IEnumerable GetSugPoDtl(int sugNum)
        {
            return from sugpodtl in DB.SugPoDtl where  sugpodtl.SugNum == sugNum select sugpodtl;
        }
        public static IEnumerable GetSugPoDtl(int sugNum,int reqNum)
        {
            return from sugpodtl in DB.SugPoDtl where sugpodtl.SugNum == sugNum && sugpodtl.ReqNum == reqNum select sugpodtl;
        }

        public static IEnumerable GetSugPoDtl2()
        {
           
            return from sugpodtl in DB.SugPoDtl where  sugpodtl.CheckBox05 == false select sugpodtl;
        }



        internal static void DeleteSugPoDtl(int sugNum)
        {
			SugPoDtl _SugPoDtl =DB.SugPoDtl.First(c => c.SugNum == sugNum);
			DB.SugPoDtl.DeleteOnSubmit(_SugPoDtl);
			DB.SubmitChanges();
        }

        internal static void UpdateCheckBox05(int sugnum)
        {
            SugPoDtl spdtl =
                    DB.SugPoDtl.First(c => c.SugNum == sugnum);
            spdtl.CheckBox05 = true;
            spdtl.ShortChar05 = "no order";
            DB.SubmitChanges();
        }
        internal static void UpdateCheckBox04(int sugnum)
        {
            SugPoDtl spdtl =
                    DB.SugPoDtl.First(c => c.SugNum == sugnum);
            spdtl.CheckBox04 = true;
            
            DB.SubmitChanges();
        }

        internal static void UpdateCheckBox05(string[] nums)
        {
            //int[] sugnums = Array.ConvertAll<string, int>(nums, delegate (string s) { return int.Parse(s); });
            var iq = from sugpodtl in  DB.SugPoDtl where nums.Contains(sugpodtl.SugNum.ToString()) select sugpodtl; // 
            foreach (SugPoDtl t in iq)
            {
                t.CheckBox05 = true;
                //t.ShortChar05 = "order";
            }
            DB.SubmitChanges();
        }
    }
}